<?php
require('../cofd/functions.php');
require('set.php');
$jm = encrypt($_GET['zj'],'D','xhpayencrypt');
$directory = $_SERVER['DOCUMENT_ROOT'].'/'.$jm;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>User-Center</title>
<link href="/ast/layui/css/layui.css" rel="stylesheet">
<script src="/ast/layui/layui.js"></script>
</head>
<body>
<style>
.greenzt{
  color:green;
}
.redzt{
  color:red;
}
.img_book{
  width:100px;
  height:150px;
  border-radius:10px;
  object-fit: cover;
  border:1px solid #ccc;
  padding:2px;
  margin:5px;
}
.booklist{
  overflow-y: scroll;
}
</style>
<section class="rt_wrap content mCustomScrollbar">
 <div style="display: none;" class="rt_content">
  <div class="booklist">
 <div class="admin_tab_cont" style="display:block;">
       <div class="cont_col_lt mCustomScrollbar" style="height:500px;width:100%">
           <table class="table fl">
            <tr>
             <th>章节</th>
             <th>操作</th>
            </tr>
            <tr>
            <?php
if (is_dir($directory)) {
    $files = scandir($directory);
    $files = array_diff($files, array('.', '..'));
    usort($files, function($a, $b) {
        preg_match('/第(\d+)章/', $a, $matchA);
        preg_match('/第(\d+)章/', $b, $matchB);
        if (isset($matchA[1], $matchB[1])) {
            return intval($matchA[1]) - intval($matchB[1]);
        }
        return strcmp($a, $b);
    });

    $newChapterNumber = 1; 
    foreach ($files as $index => $file) {
        preg_match('/第(\d+)章/', $file, $matches);
        if (isset($matches[1])) {
            if ($matches[1] == $newChapterNumber) {
                $newChapterNumber++;
            } else {
                $extension = pathinfo($file, PATHINFO_EXTENSION);
                $filenameout = pathinfo($file, PATHINFO_FILENAME);
                $filenameout2 = preg_replace('/第(\d+)章/', '', $filenameout);
                $newFileName = "第{$newChapterNumber}章" . $filenameout2.'.'.$extension;
                rename($directory . DIRECTORY_SEPARATOR . $file, $directory . DIRECTORY_SEPARATOR . $newFileName);
                $files[$index] = $newFileName;
                $newChapterNumber++;
            }
        } 
    }
    foreach ($files as $file) {
        $filename = pathinfo($file, PATHINFO_FILENAME);
        echo '<td class="center">' . htmlspecialchars($filename) . '</td>';
        echo '<td class="center">
             <a onclick="gowrite(`'.$jm.'`,`'.$filename.'`)" class="inner_btn">修改</a>
             <a onclick="delcheaper(`'.$jm.'`,`'.$filename.'`)" class="inner_btn">删除</a>
             </td>';
             echo'
            </tr>
            <tr>';
    }
} else {
    echo "目录不存在，请联系管理员解决";
}

?>
            </tr>
           </table>
       </div>
</div>
          </div>
          <a class="link_btn" id="newcheaper">新章节</a>
        </div>
</section>
</body>
</html>
<script>
  var userid = sessionStorage.getItem('user');
  fetch('/cofd/writer.php?userid='+userid)
  .then(response => response.json())
  .then(data => {
    if(data.code === 200){
      document.querySelector('.rt_content').style.display = 'block';
    }else{
      Qmsg.error(data.msg);
      document.querySelector('.rt_content').remove();
    }
  })
  </script>
  <script>
	(function($){
		$(window).load(function(){
			$("a[rel='load-content']").click(function(e){
				e.preventDefault();
				var url=$(this).attr("href");
				$.get(url,function(data){
					$(".content .mCSB_container").append(data); 
					$(".content").mCustomScrollbar("scrollTo","h2:last");
				});
			});		
			$(".content").delegate("a[href='top']","click",function(e){
				e.preventDefault();
				$(".content").mCustomScrollbar("scrollTo",$(this).attr("href"));
			});	
		});
	})(jQuery);
</script>
<script>
    var jfilepath = '<?php echo $directory;?>';
    var numcheapernow = '<?php echo $newChapterNumber;?>';
    function newcheaper(){
  layui.use(['element'], function(){
            var element = layui.element;
            layer.open({
                type: 2,
                title: '添加新内容-章节',
                shadeClose: true,
                shade: 0.8,
                area: ['900px', '80%'],
                shadeClose: false,
                content: 'newcheaper.php?cheaper='+ jfilepath+'&nownum='+numcheapernow
            });   
        });
 }
 document.getElementById('newcheaper').addEventListener('click',newcheaper);
 function gowrite(cheapername,filename){
    layui.use(['element'], function(){
            var element = layui.element;
            layer.open({
                type: 2,
                title: '编辑章节内容',
                shadeClose: true,
                shade: 0.8,
                area: ['900px', '90%'],
                shadeClose: false,
                content: 'write.php?cheaper='+cheapername+'&chep2='+filename 
            });   
        });
 }
 function delcheaper(cheapername,filename){
  if(!confirm('确定要删除该章节吗？')){
      return false;
  }
    fetch('ht/delcheaper.php?cheaper='+cheapername+'&chep2='+filename)
    .then(response => response.json())
    .then(data => {
        if(data.code===200){
            layer.msg('删除成功', {icon: 6});
            setTimeout(function(){
                window.location.reload();
            },2000);
        }else{
            layer.msg('删除失败', {icon: 5});
        }
    })
 }
  </script>

